import 'package:bitcoin_mine/api/api.dart';
import 'package:bitcoin_mine/model/withdraw_info.dart';
import 'package:get/get.dart';

import '../http/http_request.dart';

class WalletController extends GetxController {
  // 当前余额
  final RxString currentBalance = '0.000000100000000'.obs;

  // 提现历史标签
  final RxInt selectedTab = 0.obs; // 0: WITHDRAWING, 1: PAID

  // 提现记录
  final RxList<WithdrawInfo> withdrawalRecords = <WithdrawInfo>[].obs;

  @override
  void onInit() {
    super.onInit();
    getWithdrawalRecords();
  }

  // 获取提现记录
  void getWithdrawalRecords() {
    HttpRequest.instance.requestNetwork<List<WithdrawInfo>>(
      Api.withdrawList,
      method: "GET",
      onSuccess: (value) {
        if (value != null) {
          withdrawalRecords.value = value;
        }
      },
      onError: (error, message) {
        print('获取提现记录失败: $error - $message');
      },
    );
  }

  // 标签列表
  final List<String> tabs = ['WITHDRAWING', 'PAID'];

  // 获取当前标签的提现记录
  List<WithdrawInfo> get currentTabRecords {
    if (selectedTab.value == 0) {
      return withdrawalRecords.where((item) => item.status == 1).toList();
    } else {
      return withdrawalRecords.where((item) => item.status == 2).toList();
    }
    // return withdrawalRecords;
  }

  // 选择标签
  void selectTab(int index) {
    selectedTab.value = index;
  }

  // 提现
  void withdraw() {
    Get.toNamed('/withdraw');
  }
}
